var tgd = tgd || {};
tgd.Contests = tgd.Contests || {};
tgd.Contests.Darts = tgd.Contests.Darts || {};

/*
 * numDarts - ...
 * regions - array of region values, a 0 value must be included
 */
tgd.Contests.Darts.Solution = function (numDarts, regions) {
	this._numDarts = numDarts;
	this._regions = regions;
	this._numRegions = this._regions.length;

	if (tgd.Contests.Darts.Solution._initialized === undefined) {

		tgd.Contests.Darts.Solution.prototype.getNumDarts = function() {
			return this._numDarts;
		}

		tgd.Contests.Darts.Solution.prototype.getNumRegions = function() {
			return this._numRegions - 1;  //implicit 0 region
		}

		//todo:  abstract this out and only call it once to ever get the value of this, i.e. this._value = this._value || getSm...
		tgd.Contests.Darts.Solution.prototype.getSmallestScoreUnattainable = function() {
			var idx;
			var t = new Array(Math.floor(Math.pow(this._numRegions, this._numDarts)));
			for (var i = 0; i < t.length; ++i) {
				t[i] = 0;
				for (var j = 0; j < this._numDarts; ++j) {
					t[i] += this._regions[Math.floor(i / Math.pow(this._numRegions, j)) % this._numRegions];
				}
			}

			t.sort(function(a, b) {return a - b;});

			for (idx = 1; idx < t.length && t[idx -1] >= t[idx] - 1; ++idx);

			return t[idx - 1] + 1;
		}

		tgd.Contests.Darts.Solution.prototype.toString = function() {
			var retVal = this._numDarts + ":";

			this._regions.sort(function(a, b) {return a - b;});

			for (var i = 1; i < this._numRegions; ++i) {
				retVal += this._regions[i] + ",";
			}

			retVal = retVal.substr(0,retVal.length - 1) + ";";

			return retVal;
		}

		tgd.Contests.Darts.Solution._initialized = true;
	}
};